﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;

namespace Xejen.Communication
{
    /// <summary>
    /// 表示通讯返回的数据结构对象基类
    /// </summary>
    /// <creator>marc</creator>
    public class Result : IResult
    {
        /// <summary>
        /// 表示成功的编码值
        /// </summary>
        public const int SuccessCode = 200;

        /// <inheritdoc/>
        public bool IsSuccess => Code == 200;

        /// <inheritdoc/>
        public Exception Exception { get; set; }

        /// <inheritdoc/>
        public int Code { get; set; }

        /// <inheritdoc cref="Result"/>
        public Result()
        {

        }

        /// <inheritdoc/>
        public virtual object GetSerializableObject()
        {
            if (this.IsSuccess)
            {
                return new
                {
                    Code = this.Code,
                    IsSuccess = this.IsSuccess,
                };
            }
            else
            {
                return new
                {
                    Code = this.Code,
                    IsSuccess = this.IsSuccess,
                    ErrorMessage = this.Exception?.Message,
                    StackTrace = this.Exception?.StackTrace
                };
            }
        }
    }
}
